/*
    This file is part of Starlight Engine.

    Starlight Engine is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    Starlight Engine is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Starlight Engine.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using StarlightEngine;

namespace StarlightEngine
{
    /// <summary>
    /// Manages input globally.
    /// </summary>
    public static class InputManager
    {
#if ZUNE
        static AccelerometerInput accel;
        /// <summary>
        /// Gets the accelerometer controller.
        /// </summary>
        /// <value>The accelerometer.</value>
        public static AccelerometerInput Accelerometer
        {
            get { return accel; }
        }

        public static Orientation Orientation
        {
            get { return accel.Orientation; }
        }

        static TouchInput touch;
        /// <summary>
        /// Gets the touch controller.
        /// </summary>
        /// <value>The touch.</value>
        public static TouchInput Touch
        {
            get { return touch; }
        }
#endif
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public static void Initialize()
        {
           
#if ZUNE          
            touch = new TouchInput();
            accel = new AccelerometerInput();
#endif
            Logger.Write("InputManager Initialized.", 2);
        }

        /// <summary>
        /// Updates this instance.
        /// </summary>
        /// <summary>
        /// Updates this instance.
        /// </summary>
        public static void Update()
        {
#if ZUNE
            touch.Update(Engine.GameTime);
            accel.Update(Engine.GameTime);
#endif
        }

        /// <summary>
        /// Draws the debug information.
        /// </summary>
        public static void DrawDebug()
        {
#if ZUNE
            try
            {
                //HACK
                //MEGAHACK
                //FRAMES ARE DRWAN HERE FOR EASE OF CALCULATION.
                string output = "Total Touches : " + touch.TotalTouches.ToString() + "\nAccelerometer : " + accel.Acceleration.ToString() + "\nFrames : " + Engine.Frames.ToString() +"\nOrientation : " + accel.Orientation.ToString();
                
                float rotation = 0f;
                Vector2 pos = Settings.DebugTextOffset;
                int height;
                height = (int)Engine.DebugFont.MeasureString(output).Y;

                if (InputManager.Orientation == Orientation.LandscapeLeft)
                {                    
                    pos = new Vector2(0 + height, 0);
                    rotation = MathHelper.ToRadians(90);
                }
                else if (InputManager.Orientation == Orientation.LandscapeRight)
                {
                    rotation = MathHelper.ToRadians(-90);
                    int x = 272 - height;
                    int y = 480;
                    pos = new Vector2(x, y);

                }
                else if (InputManager.Orientation == Orientation.Flat || InputManager.Orientation == Orientation.NormalUpright || InputManager.Orientation == Orientation.FlatUpsideDown)
                {
                    rotation = 0f;
                    pos = Settings.DebugTextOffset;
                }
                else if (InputManager.Orientation == Orientation.NormalUpsideDown)
                {
                    rotation = MathHelper.ToRadians(180);
                    int x = 272;
                    int y = 480;
                    pos = new Vector2(x, y);
                }
                
                //Draw the touches.
                int onTouch = 1;

                 const int offset = 70;
                for (int i = 0; i < touch.TotalTouches; i++)
                {
                    //Calculate which part of screen its on.
                    TouchLocation to = touch.GetTouch(i);
                    Vector2 finalPos;
                    if (to.Position.X < 272 / 2)
                    {
                        //left.
                        if (to.Position.Y < 240)
                        {
                            //Top left.
                            finalPos = to.Position + new Vector2(offset, offset);
                        }
                        else
                        {
                            //Bottom left.
                            finalPos = to.Position + new Vector2(offset, -offset);
                        }
                    }
                    else
                    {
                        //Right side.
                        if (to.Position.Y < 240)
                        {
                            //Top right.
                            finalPos = to.Position + new Vector2(-offset, offset);
                        }
                        else
                        {
                            //Bottom right.
                            finalPos = to.Position + new Vector2(-offset, -offset);
                        }
                    }
                    onTouch = i + 1;
                    string content = onTouch.ToString();                    
                    Engine.SpriteBatch.DrawString(Engine.DebugFont, content, finalPos, Settings.TouchDebugColor, 0f, Vector2.Zero, 0.1f + to.Pressure * 4, SpriteEffects.None, 0.00f);
                    //Increment counter.
                }               

                //Oct 7 2009
                //Moved diagnostic updates to update, culprit of lag on zune.
               
                Engine.SpriteBatch.DrawString(Engine.DebugFont, output, pos, Settings.DebugTextColor, rotation, Vector2.Zero, 0.3f, SpriteEffects.None, 0.01f);

                
            }
            catch (Exception) { }
#endif
        }
    }
}